//
//  UIView+Extend.m
//  MexicoLoan
//
//  Created by WXP on 2025/2/24.
//

#import "UIView+Extend.h"

@implementation UIView (Extend)

#pragma mark - 判断是否显示在屏幕上
- (BOOL)isShowInScreen;
{
    CGRect screenRect = [UIScreen mainScreen].bounds;
    // 转换view对应window的Rect
    CGRect rect = [self convertRect:self.frame toView:[WindowManager sharedManager].getCurrentWindow];
    if (CGRectIsEmpty(rect) || CGRectIsNull(rect)) {
        return false;
    }
    // 若view 隐藏
    if (self.hidden) {
        return false;
    }
    // 若没有superview
    if (self.superview == nil) {
        return false;
    }
    // 若size为CGrectZero
    if (CGSizeEqualToSize(rect.size, CGSizeZero)) {
        return false;
    }
    // 获取 该view与window 交叉的 Rect
    CGRect intersectionRect = CGRectIntersection(rect, screenRect);
    if (CGRectIsEmpty(intersectionRect) || CGRectIsNull(intersectionRect)) {
        return false;
    }
    return true;
}

#pragma mark - 移除全部视图
- (void)removeAllSubViews{
    while (self.subviews.count) {
        [self.subviews.lastObject removeFromSuperview];
    }
}

#pragma mark - 获取获取所在vc
- (UIViewController *)fetchVC{
    UIView * view = self;
    while (view) {
        UIResponder* nextResponder = [view nextResponder];
        if (nextResponder && [nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
        view = view.superview;
    }
    return nil;
}

#pragma mark - views 组合
+ (instancetype)initWithViews:(NSArray *)ary{
    UIView * viewReturn = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
    viewReturn.backgroundColor = [UIColor clearColor];
    if (isAry(ary)) {
        CGFloat top = 0;
        for (UIView * view in ary) {
            if (view && [view isKindOfClass:[UIView class]]) {
                if (view.height) {
                    [viewReturn addSubview:view];
                    view.top = top + view.topToUpView;
                    top = view.bottom;
                }
            }
        }
        viewReturn.height = top;
    }
    return viewReturn;
}
+ (instancetype)initWithViews:(NSArray *)ary width:(CGFloat)width{
    UIView * viewReturn = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 0)];
    viewReturn.backgroundColor = [UIColor clearColor];
    if (isAry(ary)) {
        CGFloat top = 0;
        for (UIView * view in ary) {
            if (view && [view isKindOfClass:[UIView class]]) {
                if (view.height) {
                    [viewReturn addSubview:view];
                    view.top = top + view.topToUpView;
                    view.centerX = width/2;
                    top = view.bottom;
                }
            }
        }
        viewReturn.height = top;
    }
    return viewReturn;
}


#pragma mark - 获取第一响应者
- (UIView *)fetchFirstResponder
{
    if (self.isFirstResponder) {
        return self;
    }
    for (UIView *subView in self.subviews) {
        UIView *firstResponder = [subView fetchFirstResponder];
        if (firstResponder != nil) {
            return firstResponder;
        }
    }
    return nil;
}

#pragma mark - 缩放动画
- (void)addClickAnimate{
    [self addClickAnimateWithScale:0];
}
- (void)addClickAnimateWithScale:(CGFloat)scale{
    if (scale <= 0) scale = 0.9; // 默认值
    [UIView animateWithDuration:0.16 animations:^{
        self.transform = CGAffineTransformMakeScale(scale, scale);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.16 animations:^{
            self.transform = CGAffineTransformIdentity;
        }];
    }];
}

@end
